package com.shujia.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;

import java.io.BufferedReader;
import java.io.FileReader;

public class DataToHbase {
    public static void main(String[] args) throws Exception {

        /**
         * 二级索引
         *
         * 建立列值与行键的映射关系
         * 1、将列值设计奥rowkey中
         * 2、建立索引表
         *
         */

        /*
         * 创建连接
         * 获取表对象
         * put数据
         * 关闭连接
         *
         */

        Configuration configuration = new Configuration();

        //指定zk连接地址
        configuration.set("hbase.zookeeper.quorum", "node1:2181");

        //创建连接
        HConnection connection = HConnectionManager.createConnection(configuration);


        /*
         * 创建表
         * create 'student','info'
         *
         */
        //获取表对象
        HTableInterface student = connection.getTable(TableName.valueOf("student"));


        FileReader fileReader = new FileReader("data/students.txt");
        BufferedReader reader = new BufferedReader(fileReader);

        String line;
        while ((line = reader.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            String age = split[2];
            String gender = split[3];
            String clazz = split[4];

            Put put = new Put(id.getBytes());

            put.add("info".getBytes(), "name".getBytes(), name.getBytes());
            put.add("info".getBytes(), "age".getBytes(), age.getBytes());
            put.add("info".getBytes(), "gender".getBytes(), gender.getBytes());
            put.add("info".getBytes(), "clazz".getBytes(), clazz.getBytes());


            student.put(put);

        }

        connection.close();
    }
}
